package com.alphasta.common.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpHeaders;
import java.util.HashMap;
import java.util.Map;
@Component
public class GetToken {
    private static String tokenUrl;
    private static String client_id;
    private static String client_secret;
    private static String grant_type;
    @Value("${token.tokenUrl}")
    public  void setTokenUrl(String tokenUrl) {
        GetToken.tokenUrl = tokenUrl;
    }
    @Value("${token.client_id}")
    public  void setClient_id(String client_id) {
        GetToken.client_id = client_id;
    }
    @Value("${token.client_secret}")
    public  void setClient_secret(String client_secret) {
        GetToken.client_secret = client_secret;
    }
    @Value("${token.grant_type}")
    public  void setGrant_type(String grant_type) {
        GetToken.grant_type = grant_type;
    }

    public static String getToken(){
       // String tokenUrl = tokenUrl;//"http://10.11.0.5:8881/gateway-server/ldxtOauth/token";
        //headers
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-Type", "application/json");
        //body
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put("client_id", client_id);
        requestBody.put("client_secret",client_secret);
        requestBody.put("grant_type", grant_type);

        HttpEntity<Map> requestEntity = new HttpEntity<Map>(requestBody, requestHeaders);
        RestTemplate restTemplate = new RestTemplate();
        //post
        String token = restTemplate.postForObject(tokenUrl, requestEntity, String.class);
        return token;
    }
}
